package com.app.micaihu.view.army.presenter

import com.app.base.mvp.BasePresenter
import com.app.http.NetRequestListener
import com.app.http.bean.BaseBean
import com.app.micaihu.configure.LoadType
import com.app.micaihu.manager.UserInforManager
import com.app.micaihu.utils.ApiServiceUtils.apiService
import com.app.micaihu.utils.ParamsMapUtils
import com.app.micaihu.view.bean.QuestionList
import com.app.micaihu.view.army.contract.ArmyFollowContract
import com.app.utils.biz.Biz

/**
 * 军团邀请成员 关注粉丝列表
 */
open class ArmyFollowPresenter : BasePresenter<ArmyFollowContract.View>(), ArmyFollowContract.Presenter {

    private var isRefresh = true
    private var page = 1
    private var firstLoad = true

    override fun followList() {
        if (firstLoad) {
            getView()?.showLoading()
            firstLoad = false
        }
        val params = ParamsMapUtils.commonParams
        params["page"] = page
        if (Biz.instance.uid().isNotEmpty()) {
            params["uid"] = Biz.instance.uid()
        }
        requestNetwork(apiService.armyAttention(params), object : NetRequestListener<BaseBean<QuestionList?>?> {
            override fun onSuccess(result: BaseBean<QuestionList?>?) {
                getView()?.hideLoading()
                page++
                val loadType = if (isRefresh) LoadType.TYPE_REFRESH_SUCCESS else LoadType.TYPE_LOAD_MORE_SUCCESS
                getView()?.handleFollowList(result, loadType)
            }

            override fun onFailed(e: Throwable?) {
                getView()?.hideLoading()
                val loadType = if (isRefresh) LoadType.TYPE_REFRESH_ERROR else LoadType.TYPE_LOAD_MORE_ERROR
                getView()?.handleFollowList(null, loadType)
                getView()?.onError(e)
            }
        })
    }

    override fun refresh() {
        page = 1
        isRefresh = true
        followList()
    }

    override fun loadMore() {
        isRefresh = false
        followList()
    }

}